﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.Bliss.Services;
using com.Bliss;
using com.Bliss.ServiceFacade.Post.Request;
using com.Bliss.ServiceFacade.User.Request;
using System.Configuration;

namespace mojoPortal.Web.RS.Bliss.Controls
{
    public partial class SendMessage : com.Bliss.UI.Common.UserControlBase
    {
        public int ToUserID
        {
            get
            {
                int ToUserID = 0;
                if (HttpContext.Current.Request.QueryString["ToId"] != null)
                {
                    int.TryParse(HttpContext.Current.Request.QueryString["ToId"].ToString(), out ToUserID);
                }
                return ToUserID;
            }
        }

        public string OP_SSOToken
        {
            get
            {
                return Convert.ToString(ViewState["OP_SSOToken"]);
            }
            set
            {
                ViewState["OP_SSOToken"] = value;
            }
        }
        public int OP_LoggedInUserId
        {
            get
            {
                int returnValue = 0;
                int.TryParse(Convert.ToString(ViewState["OP_LoggedInUserId"]), out returnValue);
                return returnValue;
            }
            set
            {
                ViewState["OP_LoggedInUserId"] = value.ToString();
            }
        }
        public int OP_ProfileUserId
        {
            get
            {
                int returnValue = 0;
                int.TryParse(Convert.ToString(ViewState["OP_ProfileUserId"]), out returnValue);
                return returnValue;
            }
            set
            {
                ViewState["OP_ProfileUserId"] = value.ToString();
            }
        }
        private int OP_CompanyID
        {
            get
            {
                int returnValue = 0;
                int.TryParse(Convert.ToString(ViewState["OP_CompanyID"]), out returnValue);
                return returnValue;
            }
            set
            {
                ViewState["OP_CompanyID"] = value.ToString();
            }
        }
        private string OP_UserName
        {
            get
            {
                return Convert.ToString(ViewState["OP_UserName"]);
            }
            set
            {
                ViewState["OP_UserName"] = value;
            }
        }
        private string OP_ImageName
        {
            get
            {
                return Convert.ToString(ViewState["OP_ImageName"]);
            }
            set
            {
                ViewState["OP_ImageName"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (OP_SSOToken == string.Empty)
                {
                    ExceptionMessage = "User Token propertiey not set.";
                    return;
                }
                //else if (OP_ProfileUserId == 0)
                //{
                //    ExceptionMessage = "ProfileUserId propertiey not set.";
                //    return;
                //}
                else
                {
                    IUserService service = new UserService();
                    var user = service.ValidateUserSsoToken(new ValidateUserSsoTokenRequest() { SsoToken = OP_SSOToken });
                    if (user.Data == null)
                    {
                        ExceptionMessage = "Invalid User Token provided";
                        return;
                    }
                    else
                    {
                        OP_CompanyID = user.Data.CompanyID;
                        OP_LoggedInUserId = user.Data.ID;
                        OP_UserName = user.Data.Email;
                        OP_ImageName = user.Data.ImageName;
                        OP_ProfileUserId = user.Data.ID;
                    }
                }


                errorDiv.Visible = false;
                maintable.Visible = true;
                hdnToUserId.Value = ToUserID.ToString();
                if (ToUserID == 0)
                {
                    errorDiv.Visible = true;
                    maintable.Visible = false;
                    // Code to restrict
                }
                else if (ToUserID == OP_LoggedInUserId)
                {
                    errorDiv.Visible = true;
                    maintable.Visible = false;
                }
                else
                {
                    IUserService service = new UserService();
                    var userService = service.GetUser(new GetUserRequest() { UserID = ToUserID });
                    var user = userService.Data;
                    if (user == null)
                    {
                        errorDiv.Visible = true;
                        maintable.Visible = false;
                        // Code to restrict
                    }
                    else
                    {
                        string ImageName = user.ImageName;
                        if (ImageName == "NoImage")
                        {
                            imgToUser.ImageUrl =   Page.ResolveUrl(ConfigurationManager.AppSettings["ImageUrl"].ToString() + @"/image_missing.jpg");
                        }
                        else
                        {
                            imgToUser.ImageUrl =Page.ResolveUrl(ConfigurationManager.AppSettings["ImageUrl"].ToString() + @"/" + ImageName);
                        }
                        //imgToUser.PostBackUrl = ResolveUrl("~/UserDetails.aspx?Id=" + user.ID.ToString());
                        lnkToUserName.Text = user.Name;
                        //lnkToUserName.PostBackUrl = ResolveUrl("~/UserDetails.aspx?Id=" + user.ID.ToString());
                    }
                }

            }
        }
        protected void ButtonSend_Click(object sender, EventArgs e)
        {
            string message = GetLineBreaks(TextBoxScrap.Text);
            IPostService service = new PostService();
            int ToId = 0;
            int.TryParse(hdnToUserId.Value, out ToId);
            if (ToId != 0)
            {
                service.SendPrivateMessage(new SendPrivateMessageRequest { FromUserID = OP_LoggedInUserId, ParentPostID = null, ToUserID = ToId, Message = message });
                Response.Redirect("~/RS/Bliss/PrivateMessage.aspx");
            }
            else
            {
                errorDiv.Visible = true;
                maintable.Visible = false;
                // Code to restrict
            }

        }
    }
}